package com.emapp.lccode.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.emapp.lccode.activity.ProjectInfoActivity
import com.emapp.lccode.activity.ProjectMapActivity
import com.emapp.lccode.bean.Project
import com.emapp.lccode.databinding.ListitemProjectBinding
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.toJsonStr

class ProjectAdapter : BaseQuickAdapter<Project, ProjectAdapter.VH>() {

    class VH(
        parent: ViewGroup,
        val binding: ListitemProjectBinding = ListitemProjectBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        return VH(parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: Project?) {
        if (item == null) return
        holder.binding.tvName.text = item.sort_name
        holder.binding.tvYear.text = item.year
        holder.binding.tvInfo.setOnClickListener {
            val intent = Intent(context, ProjectInfoActivity::class.java)
//            intent.putExtra("infor", items[position])
            MmkvUtil.putString("infor",items[position].toJsonStr())
            context.startActivity(intent)
        }
        holder.binding.tvSee.setOnClickListener {
            val intent = Intent(context, ProjectMapActivity::class.java)
            intent.putExtra("id", item.id)
            intent.putExtra("name", item.sort_name)
            intent.putExtra("map", item.json_name)
            context.startActivity(intent)
        }
    }

}